<template>
  <div class="login">
    <div class="bgp"></div>
    <div class="login_container">
      <h1>管理系统</h1>
      <!-- 数据模型是userInfo
           数据校验规则是rules
           表单的enter事件onLogin
           将整个表单赋值给ref_form
      -->
      <el-form
        class="login_form"
        :model="userInfo"
        :rules="rules"
        @keyup.enter="onLogin"
        ref="ref_form"
      >
        <!-- prop 告知当前item所绑定的键 -->
        <el-form-item prop="userName">
          <!-- v-model.trim 数据的双向绑定 并且将输入框中的空格 删除掉 -->
          <el-input placeholder="请输入用户名" v-model.trim="userInfo.userName">
            <template #prepend>
              <el-icon><Avatar /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <!-- show-password 可以使得输入的密码变成小点 -->
          <el-input
            placeholder="请输入密码"
            show-password
            v-model.trim="userInfo.password"
          >
            <template #prepend>
              <el-icon><key /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-button
          type="primary"
          color="#626aef"
          class="login_submit"
          @click="onLogin"
          >登录</el-button
        >
        <el-button type="primary" color="#fd6c92" class="register" @click="toGo"
          >注册</el-button
        >
        <!-- <div class="register">注册</div> -->
      </el-form>
    </div>
  </div>
</template>
<script setup>
import router from '../router/index';
import { reactive, ref } from 'vue'; //导入 reactive ref两个方法
import { getLogin } from '../api/index';
/**
 * 初始ref
 */
const ref_form = ref(null); //右边的ref是响应式的 左边的ref_form是从页面获取的表单元素

/**
 * 表单数据声明
 */
const userInfo = reactive({
  userName: '',
  password: '',
});

/**
 * 表单数据校验规则
 */

const rules = reactive({
  userName: [{ required: 'true', trigger: 'blur', message: '用户名不能为空' }],
  password: [{ required: 'true', trigger: 'blur', message: '密码不能为空' }],
});

/**
 * 登录的方法
 */

const onLogin = () => {
  // 校验输入框中是否为空
  ref_form.value.validate((val) => {
    if (val) {
      // 若val==true 则代表数据填写完毕,则调用用户登录接口
      getLoginData();
    }
  });
};
/**
 * 登录的接口
 */

const getLoginData = async () => {
  //TODO: 调用登录的接口
  const res = await getLogin({
    userName: userInfo.userName,
    password: userInfo.password,
  });

  if (res?.token) {
    localStorage.setItem('token', res.token);

    router.push('/home');
  }

  // 接口的定义

  // 登录成功的提示信息
};

/**
 * 跳转去注册的页面
 */
const toGo = () => {
  router.push('/register');
};
</script>

<style lang="less" scoped>
@-webkit-keyframes fadenum {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}

.login {
  height: 100%;

  .bgp {
    height: 100%;
    // background-image: url('../assets/bj.jpeg');
    background-repeat: no-repeat;
    background-size: 100%;
    background-position: 50% 50%;
  }

  .el-icon {
    height: 30px !important;
  }

  .login_container {
    animation: fadenum 1s ease;
    position: absolute;
    z-index: 1;
    width: 400px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: all 1s;
    min-height: 273px;
    text-align: center;
    background-color: #fff;
    border-radius: 10px;
    padding: 20px;

    h1 {
      font-size: 24px;
      transition: all 1s;
      font-weight: bold;
      margin-bottom: 36px;
    }

    .el-card {
      border-radius: 20px;
    }

    .login_form {
      .login_submit {
        // width: 100%;
        // height: 50px;
        // border-radius: 20px;
        // margin-bottom: 10px;
      }

      .login_register {
        // width: 6.25rem;
        // font-size: 0.875rem;
        // margin: 0 auto;
      }

      :deep(.el-input-group__prepend) {
        padding: 0 10px;
      }
    }
  }
}
</style>